<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/6/4 0004
 * Time: 13:46
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Set extends Middle
{


    //todo banner列表
    public function BannerList(){
        $list = Db::name('article')->select();
        return $this->returnSuccess($list);
    }
    //todo banner添加
    public function BannerSet(){
        $set = [
            'banner'    =>$this->data['banner'],
            'title'     =>$this->data['title'],
            'content'   =>$this->data['content'],
            'show'      =>$this->data['show'],
        ];
        if(isset($this->data['id'])){
            $set['id'] = $this->data['id'];
            $res = Db::name('article')->update($set);
        }else{
            $set['create_time'] = time();
            $res = Db::name('article')->insert($set);
        }
        return $this->DelRedisReturn($res,'getbanner');
    }
    //todo banner详情
    public function BannerDetail(){
        $map = ['id'=>$this->data['id']];
        $res = Db::name('article')->where($map)->find();
        if(!$res)return $this->returnError($this->NoData);
        return $this->returnSuccess($res);
    }
    //todo banner删除
    public function BannerDel(){
        $map = ['id'=>$this->data['id']];
        $res = Db::name('article')->where($map)->find();
        if(!$res)return $this->returnError($this->NoData);
        $ref = Db::name('article')->where($map)->delete();
        return $this->DelRedisReturn($ref,'getbanner');
    }
    //todo 公共设置
    public function CommonList(){
        $list = Db::name('common')
            ->where(['type'=>['>',80]])
            ->field('content,id,desc')
            ->select();
        foreach ($list as $k=>$v){
            $res = preg_match('/http:\/\/(.*?).jpeg/',$v['content']);
            $type = 'string';
            if($res)$type = 'img';
            $list[$k]['type'] = $type;
        }
        return $this->returnSuccess($list);
    }
    //todo 公共设置详情
    public function CommonDetail(){
        $map = ['id'=>$this->data['id']];
        $res = Db::name('common')->where($map)->find();
        if(!$res)return $this->returnError('不存在该项');
        return $this->returnSuccess($res);
    }

    //todo 富文本
    public function FuWenBenContent(){
        $list = Db::name('common')
            ->where(['id'=>$this->data['id']])
            ->value('content');
        return $this->returnSuccess($list);
    }

    //todo 公共设置修改
    public function CommonSet(){
        $map = ['id'=>$this->data['id']];
        $res = Db::name('common')->where($map)->find();
        if(!$res)return $this->returnError('不存在该项');
        $map['content'] = $this->data['content'];
        $ref = Db::name('common')->update($map);
        if(!$ref)return $this->returnError('操作失败');
        $this->redis->delete('NiuXiong');
        $this->redis->delete('ZhiNeng');
        return $this->returnSuccess(1,'设置成功');

    }

    //todo 删除指定redis
    private function DelRedisReturn($ref,$key){
        if(!$ref)return $this->returnError($this->Error);
        $this->redis->delete($key);
        return $this->returnSuccess(null,'操作成功');
    }

    public function BannerFile(){
        $data = Request::instance()->file('data');
        if(!is_object($data))return $this->returnError('请上传文件');
        return $this->putFile($data->getInfo());
    }


}